﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows;
using SilverlightDebateViewModel.SilverlightDebateWS;
using System.Windows.Input;

namespace SilverlightDebateViewModel
{
    public class OpinionViewModel : INotifyPropertyChanged
    {
        public OpinionViewModel()
        {
            SupportOpinionCommand = new DelegateCommand(SupportOpinion, CanSupportOpinion);
            _ObjOpinion = new Opinion();
        }

        // Properties 

        #region OpinionObject
        private Opinion _ObjOpinion;
        public Opinion ObjOpinion
        {
            get { return _ObjOpinion; }
            set
            {
                if (ObjOpinion == value)
                {
                    return;
                }
                _ObjOpinion = value;
                this.NotifyPropertyChanged("ObjOpinion");
            }
        }

        #endregion

        #region VMMainPageViewModel
        private MainPageViewModel _VMMainPageViewModel;
        public MainPageViewModel VMMainPageViewModel
        {
            get { return _VMMainPageViewModel; }
            set
            {
                if (VMMainPageViewModel == value)
                {
                    return;
                }
                _VMMainPageViewModel = value;
                this.NotifyPropertyChanged("VMMainPageViewModel");
            }
        }
        #endregion

        #region VMStatementViewModel
        private StatementViewModel _VMStatementViewModel;
        public StatementViewModel VMStatementViewModel
        {
            get { return _VMStatementViewModel; }
            set
            {
                if (VMStatementViewModel == value)
                {
                    return;
                }
                _VMStatementViewModel = value;
                this.NotifyPropertyChanged("VMStatementViewModel");
            }
        }
        #endregion

        #region Visibility_Opinion
        private Visibility _Visibility_Opinion = Visibility.Collapsed;
        public Visibility Visibility_Opinion
        {
            get { return _Visibility_Opinion; }
            set
            {
                if (Visibility_Opinion == value)
                {
                    return;
                }
                _Visibility_Opinion = value;
                this.NotifyPropertyChanged("Visibility_Opinion");
            }
        }
        #endregion

        //Icommands
        #region SupportOpinionCommand
        public ICommand SupportOpinionCommand { get; set; }
        private bool CanSupportOpinion(object param)
        {
            try
            {
                return _ObjOpinion != null;
            }
            catch
            {
                return false;
            }
        }
        public void SupportOpinion(object param)
        {
            string baseAddress = VMStatementViewModel.VMMainPageViewModel.BaseAddressProperty;
            RIAAuthenticationHeader riaHeader = VMStatementViewModel.VMMainPageViewModel.RIAAuthenticationHeaderProperty;

            DebateModel.SupportOpinion(_ObjOpinion._StatementID, _ObjOpinion._OpinionID, riaHeader, baseAddress, (sender, EventArgs) =>
                {
                    if (EventArgs.Error == null)
                    {
                        this._ObjOpinion = EventArgs.Result;

                        this._VMStatementViewModel.ReloadStatement(null);
                    }
                    else
                    {
                        //BUGBUG ADD error handling message
                    }
                });
        }

        #endregion


        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}